//
// Created by 吴喆 on 2020/3/24.
//

#ifndef SUMMER_PARTICLEGENERATOR_H
#define SUMMER_PARTICLEGENERATOR_H

#include <vector>
#include <glm/glm.hpp>
#include "Texture.h"
#include "GameObject.h"
#include "Playable.h"

struct Particle {
    glm::vec2 position, velocity;
    glm::vec4 color;
    float life;

public:
    Particle() : position(0.0f), velocity(1.0f), color(1.0f), life(0.0f) {}
};

class ParticleGenerator {
public:
    ParticleGenerator(Texture2D *texture, Playable *parent, unsigned int particleCount = 100);

    virtual void update();

    virtual void render(Renderer &renderer);

protected:
    virtual unsigned int firstUnusedParticle();

    virtual void respawnParticle(Particle &particle, Playable *object, glm::vec2 offset);

    /**
     * parent
     */
    Playable *parent;

    /**
     * store particles
     */
    std::vector<Particle> particles;
    unsigned int particleCount;
    unsigned int lastUsedParticle = 0;
private:

    /**
     * texture
     */
    Texture2D *texture;

    /**
     * the count of new particle each update time.
     */
    unsigned int newParticlesEachTime = 2;
};


#endif //SUMMER_PARTICLEGENERATOR_H
